package cn.demoxy.controller;

import cn.demoxy.annotation.Log;
import cn.demoxy.enums.BusinessTypeEnum;
import cn.demoxy.model.dto.DemoMenuDTO;
import cn.demoxy.model.entity.DemoMenu;
import cn.demoxy.result.R;
import cn.demoxy.service.IDemoMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;


/**
 * @auther demo
 * @date 2022/5/20 9:37
 * 菜单表控制层
 */
@Api(tags = "菜单接口信息")
@RestController
@RequestMapping("/admin/demo-menu")
@AllArgsConstructor
@CrossOrigin
public class DemoMenuController {

    /**
     * 菜单service
     */
    private IDemoMenuService menuService;

    @ApiOperation(value = "查询所有菜单以及子菜单")
    @PostMapping("/tree")
    @RequiresPermissions("demo-menu:tree")
    public R tree() {
        return R.ok(menuService.findAllMenu());
    }


    /**
     * 根据角色id获取对应菜单
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色id获取对应菜单")
    @GetMapping("/getMenuByRoleId")
    @RequiresPermissions("demo-menu:getMenuByRoleId")
    public R getMenuByRoleId(String roleId) {
        return R.ok(menuService.getMenuByRoleId(roleId));
    }

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    @Log(title = "菜单管理菜单新增",businessType = BusinessTypeEnum.INSERT)
    @RequiresPermissions("demo-menu:save")
    public R save(@RequestBody DemoMenu menu) {
        return R.ok(menuService.save(menu));
    }

    /**
     * 修改菜单
     *
     * @param
     * @return
     */
    @ApiOperation(value = "修改菜单")
    @PostMapping("/update")
    @Log(title = "菜单管理菜单信息修改",businessType = BusinessTypeEnum.UPDATE)
    @RequiresPermissions("demo-menu:update")
    public R update(@RequestBody DemoMenu menu) {
        return R.ok(menuService.updateById(menu));
    }

    /**
     * 删除菜单
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除菜单")
    @PostMapping("/delete/{id}")
    @Log(title = "菜单管理菜单删除",businessType = BusinessTypeEnum.DELETE)
    @RequiresPermissions("demo-menu:delete")
    public R delete(@PathVariable("id") String id) {
        return R.ok(menuService.deleteMenuById(id));
    }


}
